
/**
 * Project Name:cats-core
 * File Name:User.java
 * Package Name:com.catsic.core.entity
 * Date:2014年2月26日下午3:28:19
 * Copyright (c) 2014, oa.catsic.com All Rights Reserved.
 */
 
package com.catsic.core.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.joda.time.DateTime;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.catsic.base.BaseEntity;
import com.catsic.core.status.UserStatus;


/**
 * ClassName: User
 * Description: 用户实体类
 * date: 2014年2月26日 下午3:28:19
 * author: yangyd
 */
@Entity
@Table(name="S_USER")
public class User extends BaseEntity implements Serializable,UserDetails{
	
	private static final long serialVersionUID = 5815540226438596487L;
	
	/**
	 * username: 登录昵称
	 */
	@Column(name="USERNAME",nullable=false,length=30,unique=true)
	private String username;
	
	/**
	 * password:登录密码
	 */
	@Column(name="PASSWORD",nullable=false)
	private String password;
	
	/**
	 * realname: 真实姓名
	 */
	@Column(name="REALNAME",nullable=false,length=30)
	private String realname;
	
	/**
	 * identity: 身份证号
	 */
	@Column(name="IDENTITIED")
	private String identity; 

	/**
	 * mobile:手机号码
	 */
	@Column(name="MOBILE")
	private String mobile;
	
	/**
	 * email:邮箱
	 */
	@Column(name="EMAIL")
	private String email;
	
	/**
	 * 登录失败次数
	 */
	@Column(name="FAILURE_COUNT")
	private Integer failureCount = 0;
	
	/**
	 * 最后登录时间
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	@Column(name="LAST_LOGIN")
	private Date lastLogin ;
	
	/**
	 * 密码最后修改时间
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")  
	@Column(name="PWD_LAST_UPDATE")
	private Date pwdLastUpdate;
	
	/**
	 * organ:机构
	 */
	@ManyToOne(optional=false,fetch=FetchType.LAZY)
	@JoinColumn(name="OID",referencedColumnName = "ID")
	private Organ organ;
	
	/**
	 * depart:部门
	 */
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="DID",referencedColumnName = "ID")
	private Depart depart;
	
	/**
	 * roles:角色
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "S_USER_ROLE",   
    joinColumns ={@JoinColumn(name = "USERID", referencedColumnName = "ID") },   
    inverseJoinColumns = { @JoinColumn(name = "ROLEID", referencedColumnName = "ID")})
	@Cache (usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)  
	private List<Role> roles;
	
	@Enumerated(EnumType.STRING)
	@Column(name="STATUS")
	private UserStatus status = UserStatus.激活;
	
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getIdentity() {
		return identity;
	}

	public void setIdentity(String identity) {
		this.identity = identity;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	public Integer getFailureCount() {
		return failureCount;
	}

	public void setFailureCount(Integer failureCount) {
		this.failureCount = failureCount;
	}

	public DateTime getLastLogin() {
		return null == lastLogin ? null : new DateTime(lastLogin);
	}

	public void setLastLogin(Date lastLogin) {
		this.lastLogin = lastLogin;
	}

	public DateTime getPwdLastUpdate() {
		return null == pwdLastUpdate ? null : new DateTime(pwdLastUpdate);
	}

	public void setPwdLastUpdate(Date pwdLastUpdate) {
		this.pwdLastUpdate = pwdLastUpdate;
	}

	public Organ getOrgan() {
		return organ;
	}

	public void setOrgan(Organ organ) {
		this.organ = organ;
	}

	public Depart getDepart() {
		return depart;
	}

	public void setDepart(Depart depart) {
		this.depart = depart;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public UserStatus getStatus() {
		return status;
	}

	public void setStatus(UserStatus status) {
		this.status = status;
	}

	/**
	 * 获取权限集合
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		List<GrantedAuthority> List = new ArrayList<GrantedAuthority>();
		for(Role role:this.getRoles()){
			List.add(new SimpleGrantedAuthority(role.getName()));
		}
		return List;
	}
	
	/**
	 * 获取返回值，表示没有过期
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	@Override
	public boolean isAccountNonExpired() {
		return true;
		
	}
	
	/**
	 * TODO 获取返回值，表示没有过期
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	@Override
	public boolean isAccountNonLocked() {
		return true;
		
	}
	
	/**
	 * 获取返回值，表示用户密码没有过期
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	@Override
	public boolean isCredentialsNonExpired() {
		return true;
		
	}
	
	/**
	 * TODO 是否禁用
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		return true;
		
	}

}
